<template>
  <view class="register-container">
    <!-- 注册卡片 -->
    <view class="register-card">
      <!-- 用户类型选择 -->
      <view class="form-item">
        <text class="form-label">用户类型</text>
        <picker
          class="type-picker"
          @change="userTypeChange"
          :value="userTypeIndex"
          :range="userTypes"
          range-key="roleName"
        >
          <view class="picker-content">
            {{ userTypes[userTypeIndex].roleName }}
            <text class="cuIcon-unfold"></text>
          </view>
        </picker>
      </view>

      <!-- 动态表单内容 -->
      <template>
        <!-- 用户端表单 -->
        <view class="form-item">
          <text class="form-label">用户名</text>
          <input
            class="form-input"
            v-model="yonghuForm.username"
            placeholder="请输入用户名"
          />
        </view>
        <view class="form-item">
          <text class="form-label">密码</text>
          <input
            class="form-input"
            type="password"
            v-model="yonghuForm.password"
            placeholder="请输入密码"
          />
        </view>
        <view class="form-item">
          <text class="form-label">确认密码</text>
          <input
            class="form-input"
            type="password"
            v-model="yonghuForm.confirmPassword"
            placeholder="请确认密码"
          />
        </view>
      </template>

      <button class="register-btn" @tap="register">立即注册</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      yonghuxingbieOptions: [],
      yonghuxingbieIndex: 0,
      nonghuxingbieOptions: [],
      nonghuxingbieIndex: 0,
      ruleForm: {},
      tableName: "",
      //   userTypes: ["普通用户", "农户", "卖家"],
      userTypes: [
        {
          roleId: 102,
          roleName: "普通用户",
        },
        {
          roleId: 100,
          roleName: "农户",
        },
        {
          roleId: 101,
          roleName: "卖家",
        },
      ],
      userTypeIndex: 0,
      yonghuForm: {
        username: "",
        password: "",
        // 其他用户字段...
      },
    };
  },
  async onLoad() {
    let res = [];
    let table = uni.getStorageSync("loginTable");
    this.tableName = table;

    // 自定义下拉框值
    if (this.tableName == "yonghu") {
      this.yonghuxingbieOptions = "男,女".split(",");
      this.ruleForm.xingbie = this.yonghuxingbieOptions[0];
    }
    // 自定义下拉框值
    if (this.tableName == "nonghu") {
      this.nonghuxingbieOptions = "男,女".split(",");
      this.ruleForm.xingbie = this.nonghuxingbieOptions[0];
    }

    this.styleChange();
  },
  methods: {
    // 下拉变化
    yonghuxingbieChange(e) {
      this.yonghuxingbieIndex = e.target.value;
      this.ruleForm.xingbie =
        this.yonghuxingbieOptions[this.yonghuxingbieIndex];
    },
    // 下拉变化
    nonghuxingbieChange(e) {
      this.nonghuxingbieIndex = e.target.value;
      this.ruleForm.xingbie =
        this.nonghuxingbieOptions[this.nonghuxingbieIndex];
    },
    userTypeChange(e) {
      this.userTypeIndex = e.detail.value;
      this.yonghuForm.roleId = this.userTypes[this.userTypeIndex].roleId;
    },
    styleChange() {
      this.$nextTick(() => {
        // document.querySelectorAll('.uni-input .uni-input-input').forEach(el=>{
        //   el.style.backgroundColor = this.registerFrom.content.input.backgroundColor
        // })
      });
    },
    // 获取uuid
    getUUID() {
      return new Date().getTime();
    },
    // 注册
    async register() {
      if (!this.yonghuForm.username) {
        this.$utils.msg(`用户名不能为空`);
        return;
      }
      if (!this.yonghuForm.password) {
        this.$utils.msg(`密码不能为空`);
        return;
      }
      if (this.yonghuForm.roleId == 101) {
        this.yonghuForm.status = 1;
      }
      await this.$api.register(``, this.yonghuForm);
      this.$utils.msgBack("注册成功");
    },
  },
};
</script>

<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40rpx 32rpx;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.register-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  border-radius: 24rpx;
  padding: 48rpx 32rpx;
  width: 100%;
  max-width: 600rpx;
  box-shadow: 0 8rpx 32rpx rgba(102, 126, 234, 0.2);
}

.form-item {
  margin-bottom: 32rpx;

  .form-label {
    display: block;
    color: #667eea;
    font-size: 28rpx;
    margin-bottom: 16rpx;
    font-weight: 500;
  }
}

.type-picker {
  .picker-content {
    background: #f8f9ff;
    border: 2rpx solid #e2e8f0;
    border-radius: 48rpx;
    padding: 24rpx 32rpx;
    color: #2d3748;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 28rpx;
    transition: all 0.3s;

    &:active {
      background: rgba(102, 126, 234, 0.1);
    }
  }
}

.form-input {
  background: #f8f9ff;
  border: 2rpx solid #e2e8f0;
  border-radius: 48rpx;
  padding: 24rpx 32rpx;
  box-sizing: border-box;
  font-size: 28rpx;
  color: #2d3748;
  transition: all 0.3s;
  height: 100rpx;
  &:focus {
    border-color: #667eea;
    box-shadow: 0 0 16rpx rgba(102, 126, 234, 0.2);
  }
}

.register-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border-radius: 48rpx;
  height: 96rpx;
  font-size: 32rpx;
  margin-top: 48rpx;
  transition: all 0.2s;

  &:active {
    opacity: 0.9;
    transform: scale(0.98);
  }
}
</style>
